package cn.com.nes.site.entity.em.agent.grant;

import cn.com.nes.common.exception.NoSuchEnumException;
import cn.com.nes.site.entity.em.IEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public enum AgentGrantTypeEnum implements IEnum<String> {
    SALE("0", "销售"),
    INSTALL("1", "安装"),
    MAINTENANCE("2", "维修"),
    ;

    private final String key;

    private final String value;

    AgentGrantTypeEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    public static AgentGrantTypeEnum getEnum(String key) {
        Map<String, AgentGrantTypeEnum> map = new HashMap<>();
        for (AgentGrantTypeEnum _enum : values()) {
            map.put(_enum.getKey(), _enum);
        }
        if (!map.containsKey(key)) {
            throw new NoSuchEnumException("未匹配的授权类型");
        }
        return map.get(String.valueOf(key));
    }

    public static String enumConvertKeyToValue(String key) {
        String val = "";
        try {
            val = getEnum(key).getValue();
        } catch (NoSuchEnumException e) {
            log.warn("{}, convert key to value error, key:[{}], cause:[{}]", e.getStackTrace()[0].getFileName(), key, e.getMessage());
        }
        return val;
    }
}
